# ***************************************************************************
# *   Copyright (c) 2023 0penBrain                               *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

# This is a build and test workflow for CI of FreeCAD.
# This workflow aims at building and testing FreeCAD using a Conda environment on Windows with MSVC.

name: Build Windows (Conda)
on:
  workflow_call:
    inputs:
      artifactBasename:
        type: string
        required: true
      allowedToFail:
        default: false
        type: boolean
        required: false
    outputs:
      reportFile:
        value: ${{ jobs.Build.outputs.reportFile }}

jobs:
  Build:
    runs-on: windows-latest
    continue-on-error: ${{ inputs.allowedToFail }}
    env:
      CCACHE_DIR: C:/FC/cache/
      CCACHE_COMPILERCHECK: "%compiler%" # default:mtime
      CCACHE_MAXSIZE: 1G
      CCACHE_COMPRESS: true
      CCACHE_COMPRESSLEVEL: 1
      CCACHE_NOHASHDIR: true
      CCACHE_DIRECT: true
      #CCACHE_SLOPPINESS: "pch_defines,time_macros" # Can't get PCH to work on Windows
      CCACHE_LOGFILE: C:/logs/ccache.log
      ## Have to use C:\ because not enough space on workspace drive
      builddir: C:/FC/build/
      logdir: C:/logs/
      reportdir: C:/report/
      reportfilename: ${{ inputs.artifactBasename }}-report.md
    outputs:
      reportFile: ${{ steps.Init.outputs.reportFile }}

    steps:
      - name: Checking out source code
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Setup Miniconda
        uses: conda-incubator/setup-miniconda@v3
        with:
          activate-environment: .conda/freecad
          environment-file: conda/conda-env.yaml
          channels: conda-forge,defaults
          channel-priority: true
          miniforge-version: latest
      - name: Install FreeCAD dependencies
        run: |
          conda config --add envs_dirs $PWD/.conda
          mamba-devenv -f conda/environment.devenv.yml
      - name: Make needed directories, files and initializations
        id: Init
        run: |
          mkdir ${{ env.CCACHE_DIR }}
          mkdir ${{ env.builddir }}
          mkdir ${{ env.logdir }}
          mkdir ${{ env.reportdir }}
          echo "reportFile=${{ env.reportfilename }}" >> $GITHUB_OUTPUT
      - name: Restore compiler cache
        uses: actions/cache@v4
        with:
          save-always: true
          path: ${{ env.CCACHE_DIR }}
          key: FC-Windows-Conda-${{ github.ref }}-${{ github.run_id }}
          restore-keys: |
            FC-Windows-Conda-${{ github.ref }}-
            FC-Windows-Conda-
      - name: Print Ccache statistics before build, reset stats and print config
        run: |
          ccache -s
          ccache -z
          ccache -p
      - name: Configuring CMake
        shell: cmd /C CALL {0}
        run: >
          conda\cmake.cmd --preset conda-windows-release -DFREECAD_USE_PCH:BOOL=OFF -DFREECAD_RELEASE_PDB:BOOL=OFF -DFREECAD_USE_CCACHE:BOOL=ON
      - name: Compiling sources
        shell: cmd /C CALL {0}
        run: >
          conda\cmake.cmd --build build\release
      - name: Print Ccache statistics after build
        run: |
          ccache -s
      - name: CMake Install
        shell: cmd /C CALL {0}
        run: |
          conda\cmake.cmd  --install build\release
      - name: C++ unit tests
        timeout-minutes: 1
        run: |
          . build\release\tests\Tests_run --gtest_output=json:${{ env.reportdir }}gtest_results.json # 2>&1 | tee -filepath ${{ env.logdir }}/unitTests.log
      - name: FreeCAD CLI tests
        run: |
          . build\release\bin\FreeCADCmd -t 0 # 2>&1 | tee -filepath ${{ env.logdir }}/integrationTests.log
      - name: Upload logs
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: ${{ inputs.artifactBasename }}-Logs
          path: |
            ${{ env.logdir }}
